﻿<%@ WebHandler Language="C#" Class="markedHandler" %>

using System;
using System.Web;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Chenxin.Lib.Utility;

public class markedHandler : AjaxHandlerBase
{
    public markedHandler()
    {
        htTypePermit.Add("add", Constants.Permit_Marke_Add);
        htTypePermit.Add("edit", Constants.Permit_Marke_Edit);
        htTypePermit.Add("del", Constants.Permit_Marke_Del);
    }

    override public void RequestProcess(HttpContext context)
    {
        string strOp = context.Request["op"];
        string strResponse = "";
        switch (strOp.ToLower())
        {
            case "edit":
                strResponse = edit(context);
                break;
            case "validate":
                strResponse = validate(context);
                break;
            case "validatemarked":
                strResponse = validatemarked(context);
                break;
            case "getmodel":
                strResponse = getmodel(context);
                break;
            case "validateperson":
                strResponse = validatePerson(context);
                break;
            case "add":
                strResponse = insert(context);
                break;
            case "getlist":
                strResponse = getMarkedList(context);
                break;
            case "getmarkedlist":  //chenjie Add  2010.6.18
                strResponse = getMarkedListByHouseid(context);
                break;
            case "del":
                strResponse = DelMarkedList(context);
                break;
            default:
                break;
        }
        context.Response.Write(strResponse);
    }

    //*******************************************
    //功能概要       ：添加备案
    //参数説明       ：houseid 房源主键 markedNumber 备案号 markedTime 备案时间
    //返回值説明     ：void
    //功能詳細       ：根据房源编号获得房源信息
    //作成者         ：chen.yugong
    //作成日         ：2010-03-01
    //修改者         ：
    //修改日         ：
    //*******************************************
    private string insert(HttpContext context)
    {
        try
        {
            //转换成实体
            Chenxin.Lib.House.Entity.House_MarkedInfo model = (Chenxin.Lib.House.Entity.House_MarkedInfo)Chenxin.Lib.Utility.JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.House.Entity.House_MarkedInfo));
            Chenxin.Lib.House.Entity.House_BaseInfo house_BaseInfo = Chenxin.Lib.House.DAL.House_BaseInfo.GetModel(model.HouseId);
            if (house_BaseInfo == null)
                return string.Format("{{success:'0',msg:'{0}',data:''}}", "房源信息丢失，请联系管理员！");

            //验证是否在House_BusinessSetting表设定范围之内
            if (!CurrentUserInfo.UserId.Equals(house_BaseInfo.OprPersonId) && CurrentUserInfo.UserType != 100)
            {
                Chenxin.Lib.House.Entity.House_BusinessSetting bs = Chenxin.Lib.House.DAL.House_BusinessSetting.GetModel(CurrentUserInfo.DepartId);
                if (bs != null)
                {
                    if (bs.IsProtectMark)
                    {
                        int hours = DateTime.Now.Subtract((DateTime)house_BaseInfo.Adddate).Days * 24;
                        if (DateTime.Now.Subtract((DateTime)house_BaseInfo.Adddate).Hours + hours < bs.MarkProtectHours)
                        {
                            return string.Format("{{success:'0',msg:'{0}',data:''}}", "该房源目前在优先备案保护中");
                        }
                    }
                }
            }
            if (house_BaseInfo.TraceNum == null)
                house_BaseInfo.TraceNum = 1;
            else
                house_BaseInfo.TraceNum = house_BaseInfo.TraceNum + 1;
            house_BaseInfo.IsMarked = true;
            house_BaseInfo.MarkCode = model.MarkCode;
            house_BaseInfo.ServiceTime = DateTime.Now;
            house_BaseInfo.UpdateTime = DateTime.Now;
            Chenxin.Lib.House.DAL.House_BaseInfo.Update(house_BaseInfo);

            model.RecordId = Guid.NewGuid();
            model.HouseAddress = house_BaseInfo.Community + house_BaseInfo.BuildingName + house_BaseInfo.Room;
            model.MarkPerson = CurrentUserInfo.UserName;
            model.MarkPersonID = CurrentUserInfo.UserId;
            model.MarkDepart = CurrentUserInfo.DepartName;
            model.MarkDepartId = CurrentUserInfo.DepartId;
            model.CompanyId = CurrentUserInfo.CompanyId;
            model.CompanyName = CurrentUserInfo.CompanyName;
            model.Status = 0;
            model.AddTime = DateTime.Now;
            Chenxin.Lib.House.DAL.House_MarkedInfo.Add(model);

            //清除缓存
            string strCachKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_HOUSEINFO, house_BaseInfo.HouseId);
            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
            cs.Delete(strCachKey);

            //添加跟单记录
            Chenxin.Lib.House.Entity.House_TraceInfo house_TraceInfo = new Chenxin.Lib.House.Entity.House_TraceInfo();
            //跟单方式
            house_TraceInfo.TraceType = 0;
            //跟单内容	 
            house_TraceInfo.TraceContext = "[" + CurrentUserInfo.UserName + "]设置了备案,编号：" + model.MarkCode;
            //房源Id	 
            house_TraceInfo.HouseId = house_BaseInfo.HouseId;
            //房源编号	
            house_TraceInfo.HouseCode = house_BaseInfo.HouseCode;
            //房源地址
            house_TraceInfo.HouseAddress = model.HouseAddress;
            house_TraceInfo.status = 0;
            house_TraceInfo.OprDepart = house_BaseInfo.OprDepart;
            house_TraceInfo.OprDepartId = house_BaseInfo.OprDepartId;
            house_TraceInfo.OprPerson = house_BaseInfo.OprPerson;
            house_TraceInfo.OprPersonId = house_BaseInfo.OprPersonId;
            
            AddTrace(house_TraceInfo);

            //添加用户操作日志 2010-5-11 王玮 Add
            AddLog("" + CurrentUserInfo.UserName + "添加了一个备案:备案编号[" + model.MarkCode + "]",
                    OperationType.OperationTypeComboList()["OprType_RecordAdd"].ToString());

            return string.Format("{{success:'1',msg:'{0}',data:''}}", "备案成功！");
        }
        catch (Exception ex)
        {
            return string.Format("{{success:'0',msg:'意外错误：{0}',data:''}}", ex.Message);
        }
    }

    //*******************************************
    //功能概要       ：删除备案
    //参数説明       ：recordid 备案号id主键
    //返回值説明     ：void
    //功能詳細       ：根据房源编号获得房源信息
    //作成者         ：zou.xiaochun
    //作成日         ：2011-02-24
    //修改者         ：
    //修改日         ：
    //*******************************************
    private string DelMarkedList(HttpContext context)
    {
        try
        {
            if (context.Request["modelid"] != null)
            {
                Chenxin.Lib.House.Entity.House_MarkedInfo exhmi = Chenxin.Lib.House.DAL.House_MarkedInfo.GetModel(new Guid(context.Request["modelid"]));
                Chenxin.Lib.House.DAL.House_MarkedInfo.Delete(new Guid(context.Request["modelid"]));
                Chenxin.Lib.House.Entity.House_MarkedInfo hmi = Chenxin.Lib.House.DAL.House_MarkedInfo.GetModelByHouseId(exhmi.HouseId);

                if (hmi != null)
                {
                    Chenxin.Lib.House.DAL.House_BaseInfo.UpdateMarkedInfo(hmi.HouseId, hmi.MarkCode,true);
                }
                else
                {
                    Chenxin.Lib.House.DAL.House_BaseInfo.UpdateMarkedInfo(exhmi.HouseId, "", false);
                }

                //添加用户操作日志 2010-5-11 王玮 Add
                AddLog("" + CurrentUserInfo.UserName + "删除了一个备案:[" + exhmi.HouseAddress + "]房源已删除备案。",
                        OperationType.OperationTypeComboList()["OprType_RecordDel"].ToString());

                return "{success:'1',msg:'删除成功！',data:''}";
            }
            else
            {
                return "{success:'0',msg:'请选择一条记录！',data:''}";
            }
        }
        catch (Exception e)
        {
            return string.Format("{{success:'0',msg:'{0}',data:''}}", e.Message);
        }
    }
    
    //*******************************************
    //功能概要       ：备案号验证
    //参数説明       ：houseid 房源主键 markedNumber 备案号  
    //返回值説明     ：string
    //功能詳細       ：备案号验证
    //作成者         ：chen.yugong
    //作成日         ：2010-03-01
    //修改者         ：
    //修改日         ：
    //*******************************************
    private string validate(HttpContext context)
    {
        try
        {
            string houseId = context.Request["houseid"];
            string markedNumber = context.Request["markedNumber"];

            System.Collections.Generic.List<Chenxin.Lib.House.Entity.House_MarkedInfo> mkList = new System.Collections.Generic.List<Chenxin.Lib.House.Entity.House_MarkedInfo>();
            string strWhere = string.Format("CompanyId = '{0}' AND MarkCode = '{1}'", CurrentUserInfo.CompanyId, markedNumber);
            mkList = Chenxin.Lib.House.DAL.House_MarkedInfo.GetListArray(strWhere);
            if (mkList.Count > 0)
            {
                return string.Format("{{success:'0',msg:'{0}',data:''}}", "备案号已存在！");
            }
            else
            {
                return string.Format("{{success:'1',msg:'{0}',data:''}}", "备案号可用！");
            }
        }
        catch (Exception ex)
        {
            return string.Format("{{success:'0',msg:'意外错误：{0}',data:''}}", ex.Message);
        }
    }

    private string validatemarked(HttpContext context)
    {
        try
        {
            string recordid = context.Request["recordid"];
            string markedNumber = context.Request["MarkCode"];

            System.Collections.Generic.List<Chenxin.Lib.House.Entity.House_MarkedInfo> mkList = new System.Collections.Generic.List<Chenxin.Lib.House.Entity.House_MarkedInfo>();
            string strWhere = string.Format("CompanyId = '{0}' AND MarkCode = '{1}' and RecordId<>'{2}' ", CurrentUserInfo.CompanyId, markedNumber,recordid);
            mkList = Chenxin.Lib.House.DAL.House_MarkedInfo.GetListArray(strWhere);
            if (mkList.Count > 0)
            {
                return string.Format("{{success:'1',msg:'',data:{{exist:true}}}}");
            }
            else
            {
                return string.Format("{{success:'1',msg:'',data:{{exist:false}}}}");
            }
        }
        catch (Exception ex)
        {
            return string.Format("{{success:'0',msg:'意外错误：{0}',data:''}}", ex.Message);
        }
    }

    private string getMarkedList(HttpContext context)
    {
        try
        {
            int iPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;//页码
            int iPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);//页记录数

            string fieldlist = "RecordId,HouseId,MarkCode,MarkDay,MarkPerson,MarkDepart,HouseAddress,AddTime,MarkStartTime,MarkEndTime,PropertyCode,PropertyPersonName";
            string fieldtablelist = "RecordId,HouseId,MarkCode,MarkDay,MarkPerson,MarkDepart,HouseAddress,AddTime,MarkStartTime,MarkEndTime,PropertyCode,PropertyPersonName";
            string orderfield = (string.IsNullOrEmpty(context.Request["sort"])) ? "House_MarkedInfo.AddTime desc" : context.Request["sort"];

            Guid city = (string.IsNullOrEmpty(context.Request["cityid"])) ? Guid.Empty : new Guid(context.Request["cityid"]);
            Guid company = (string.IsNullOrEmpty(context.Request["companyid"])) ? Guid.Empty : new Guid(context.Request["companyid"]);
            Guid businessdistrict = (string.IsNullOrEmpty(context.Request["areaid"])) ? Guid.Empty : new Guid(context.Request["areaid"]);
            Guid departs = (string.IsNullOrEmpty(context.Request["departid"])) ? Guid.Empty : new Guid(context.Request["departid"]);
            Guid employee = (string.IsNullOrEmpty(context.Request["userid"])) ? Guid.Empty : new Guid(context.Request["userid"]);

            //判断权限范围/////////////////////////////////////////////
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("(1=1)");

            if (employee != Guid.Empty)
                sb.AppendFormat(" AND (MarkPersonID = '{0}') ", employee);
            else if (departs != Guid.Empty)
                sb.AppendFormat(" AND (MarkDepartId = '{0}')", departs);
            else if (businessdistrict != Guid.Empty)
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo item = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, businessdistrict);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
                item = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    item = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(businessdistrict);
                    if (item != null) cs.Set(strCachkey, item, DateTime.Now.AddDays(1));
                }

                if (item == null) return string.Format("{{success:'0',msg:'{0}',data:''}}", "获取商圈或分类数据丢失");

                sb.Append(" AND (EXISTS(");
                sb.Append("Select 1 from v_Hr_DepartmentInfo M ");
                sb.AppendFormat("WHERE (House_MarkedInfo.MarkDepartId = M.Departid AND M.DepartSrc like '{0}%'))) ", item.DepartSrc);

                //sb.AppendFormat(" AND (MarkDepartId in (select departId from v_Hr_DepartmentInfo where DepartSrc like '{0}%'))", item.DepartSrc);
            }
            else if (company != Guid.Empty)
                sb.AppendFormat(" AND (CompanyId ='{0}')", company);
            else if (city != Guid.Empty)
            {
                sb.Append(" AND (EXISTS(");
                sb.Append("Select 1 from v_Hr_DepartmentInfo M ");
                sb.AppendFormat("WHERE (House_MarkedInfo.MarkDepartId = M.Departid AND M.CityId = '{0}%'))) ", city);

                //sb.AppendFormat(" AND (MarkDepartId in (Select DepartId from v_Hr_DepartmentInfo where CityId = '{0}'))", city);
            }
            else
            {
                sb.AppendFormat(" AND (CompanyId ='{0}')", CurrentUserInfo.CompanyId);
            }

            //************逻辑范围***********************************/
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_Marke_ViewAll))
            {
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_Marke_ViewCity))
            {
                sb.Append(" AND (EXISTS(");
                sb.Append("Select 1 from v_Hr_DepartmentInfo N ");
                sb.AppendFormat("WHERE (House_MarkedInfo.MarkDepartId = N.Departid AND  N.CityId= '{0}'))) ", CurrentUserInfo.CityId);
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_Marke_ViewCompany))
            {
                sb.AppendFormat(" AND (CompanyId ='{0}')", CurrentUserInfo.CompanyId);
            }
            else
            {
                sb.Append(" AND (1 <> 1) ");
            }

            string stime = context.Request["stime"];
            string etime = context.Request["etime"];
            string strquery = context.Request["strquery"];
            string markednumber = context.Request["markednumber"];
            if (!String.IsNullOrEmpty(stime))
            {
                sb.Append(string.Format(" AND convert(varchar(10),(convert(datetime,MarkStartTime,120)),120)  >= '{0}'", (stime)));
            }
            if (!String.IsNullOrEmpty(etime))
            {
                sb.Append(string.Format(" AND convert(varchar(10),(convert(datetime,MarkEndTime,120)),120)  <= '{0}'", (etime)));
            }
            if (!String.IsNullOrEmpty(strquery))
            {
                sb.Append("AND HouseAddress LIKE '%" + strquery + "%'");
            }
            if (!String.IsNullOrEmpty(markednumber))
            {
                sb.Append(string.Format(" and MarkCode Like '%{0}%'", markednumber));
            }
            //判断权限范围结束////////////////////////////////////////////


            System.Data.DataSet list = null;
            list = Chenxin.Lib.House.DAL.House_MarkedInfo.GetList(iPageIndex, iPageSize, orderfield, "House_MarkedInfo", sb.ToString(), fieldlist, fieldtablelist, true);
            int count = (int)list.Tables[1].Rows[0][0];
            string strHtml = "";
            if (!GetKeyListHtml(list, out strHtml))
            {
                return string.Format("{{success:'0',msg:'{0}',data:''}}", "生成HTML错误:" + strHtml);
            }
            list.Dispose();
            list = null;
            return string.Format("{{success:'1',msg:'{0}',data:{{count:'{1}',html:'{2}'}}}}", "成功", count, strHtml);
        }
        catch (Exception ex)
        {
            return string.Format("{{success:'0',msg:'{0}',data:''}}", "未知错误：" + ex.Message);
        }
    }

    /// <summary>
    /// 根据HouseID获取房源备案信息-----chenjie
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    private string getMarkedListByHouseid(HttpContext context)
    {
        try
        {
            string houseid = context.Request["houseid"];
            
            int iPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;//页码
            int iPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);//页记录数
            string OrderField = "House_MarkedInfo.AddTime desc";
            string StrWhere = "HouseId='" + houseid + "'";
            string FieldList = "MarkPerson,MarkDepart,MarkDay,MarkCode,MarkStartTime,MarkEndTime";
            string TableFieldList = "MarkPerson,MarkDepart,convert(nvarchar(10),MarkDay,120) as MarkDay,MarkCode,convert(nvarchar(12),MarkStartTime,111) as MarkStartTime,convert(nvarchar(12),MarkEndTime,111) as MarkEndTime";
            string TableName = "House_MarkedInfo";
            //根据sql执行查询
            
            DataSet ds=null;
            if (iPageIndex == 1)
            {
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HOUSE);
                string strCachKey = string.Format(Chenxin.Lib.Cache.CacheDataType.HOUSE_INFO_MARKED, houseid);
                ds = cs.GetDataSet(strCachKey);

                if (!cs.Hit)
                {
                    ds = Chenxin.Lib.House.DAL.House_TraceInfo.GetList(iPageIndex, iPageSize, OrderField, TableName, StrWhere, FieldList, TableFieldList, true);
                    if (ds != null)
                        cs.Set(strCachKey, ds, DateTime.Now.AddMinutes(5));
                }
            }
            else
            {
                ds = Chenxin.Lib.House.DAL.House_TraceInfo.GetList(iPageIndex, iPageSize, OrderField, TableName, StrWhere, FieldList, TableFieldList, true);
            }
            
            StringBuilder sb = new StringBuilder();
            sb.Append("<table id=\"showBeiAn\" width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><thead>");
            sb.Append("<tr class=\"tab_line4\"><td width=\"20%\"><b>备案人</b></td><td width=\"20%\"><b>备案时间</b></td><td width=\"20%\"><b>备案号</b></td></td><td width=\"20%\"><b>备案开始时间</b></td><td width=\"20%\"><b>备案结束时间</b></td></tr>");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sb.AppendFormat("<tr class=\"tab_line5\"><td title=\"{0}/{1}\">{0}/{1}</td><td title=\"{2}\">{2}</td><td title=\"{3}\">{3}</td><td title=\"{4}\">{4}</td><td title=\"{5}\">{5}</td></tr>", dr[0], dr[1], dr[2], dr[3], dr[4], dr[5]);
            }
            sb.Append("</thead></table>");
            return string.Format("{{success:'1',msg:'{0}',data:{{count:'{1}',data:'{2}'}}}}", "成功", Convert.ToInt32(ds.Tables[1].Rows[0][0]), sb.ToString());
        }
        catch (Exception err)
        {
            return string.Format("{{success:'0',msg:'{0}',data:''}}", "生成HTML错误:" + err.Message);
        }
    }


    /// <summary>
    /// 返回列表；
    /// 根据查询参数生成查询条件1,根据权限设定生成查询条件2；
    /// 返回Json数据：{count:'30',data:'abc'}
    /// </summary>
    /// <param name="context"></param>
    private bool GetKeyListHtml(System.Data.DataSet dslist, out string strHtml)
    {
        try
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            /*
            sb.Append("<table width=\"100%\" height=\"100%\" cellspacing=\"1\" class=\"tabsorternew\" id=\"report\">");
            sb.Append("<thead><tr>");
            sb.Append("<th width=\"80px\" >备案编号</th>");
            sb.Append("<th width=\"80px\" >备案日期</th>");
            sb.Append("<th width=\"80px\" >备案开始日期</th>");
            sb.Append("<th width=\"80px\" >备案结束日期</th>");
            sb.Append("<th width=\"70px\" >业务员</th>");
            sb.Append("<th width=\"80px\" >门店</th>");
            sb.Append("<th>地址</th>");
            sb.Append("<th width=\"120px\" >添加时间</th>");
            sb.Append("</tr></thead><tbody>");*/

            foreach (System.Data.DataRow row in dslist.Tables[0].Rows)
            {
                sb.AppendFormat("<dl id=\"{0}\">", row["RecordId"].ToString());

                sb.AppendFormat("<dd>{0} {1}</dd>", string.Format("<a name=\"{0}\" class=\"a_chang\" href=\"#\">变更</a>", row["RecordId"].ToString()), 
                                                    string.Format("<a name=\"{0}\" class=\"a_delete\" href=\"#\">删除</a>", row["RecordId"].ToString()));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "MarkCode", ""));
                sb.AppendFormat("<dd><a href=\"ShowDetailInfo.aspx?houseid={1}\" target=\"blank\">{0}</a></dd>",
                   Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "HouseAddress", ""),
                   Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "HouseId", Guid.Empty));
                sb.AppendFormat("<dd>{0:yyyy-MM-dd}</dd>", Convert.ToDateTime(Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "MarkDay", "")));
                sb.AppendFormat("<dd>{0:yyyy-MM-dd}</dd>", Convert.ToDateTime(Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "MarkStartTime", "")));
                sb.AppendFormat("<dd>{0:yyyy-MM-dd}</dd>", Convert.ToDateTime(Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "MarkEndTime", "")));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "PropertyCode", ""));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "PropertyPersonName", ""));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "MarkPerson", ""));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "MarkDepart", ""));
                sb.AppendFormat("<dd>{0:yyyy-MM-dd}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "AddTime", ""));
                sb.Append("</dl>");
            }

            strHtml = sb.ToString();
            return true;
        }
        catch (Exception ex)
        {
            strHtml = ex.Message;
            return false;
        }
    }

    //*******************************************
    //功能概要       ：验证工号
    //参数説明       ：personId 工号
    //返回值説明     ：void
    //功能詳細       ：验证工号
    //作成者         ：chen.yugong 
    //作成日         ：2010-03-02
    //修改者         ：
    //修改日         ：
    //*******************************************
    private string validatePerson(HttpContext context)
    {
        try
        {
            string personId = context.Request["changPersonCode"];
            Chenxin.Lib.Passport.Entity.Hr_UserInfo userInfo = Chenxin.Lib.Passport.DAL.Hr_UserInfo.GetEntityByCode(personId);
            if (userInfo != null)
            {
                return string.Format("{{success:'1',msg:'成功',data:{{model:{0}}}}}", Chenxin.Lib.Utility.JsonHelp.Convert2Json(userInfo));
            }
            else
            {
                return string.Format("{{success:'0',msg:'{0}',data:''}}", "用户不存在！");
            }
        }
        catch (Exception ex)
        {
            return string.Format("{{success:'0',msg:'意外错误：{0}',data:''}}", ex.Message);
        }
    }

    private string getmodel(HttpContext context)
    {
        try
        {
            string modelid = context.Request["modelid"];
            Chenxin.Lib.House.Entity.House_MarkedInfo hmi = Chenxin.Lib.House.DAL.House_MarkedInfo.GetModel(new Guid(modelid));
            if (hmi != null)
            {
                Chenxin.Lib.House.Entity.House_BaseInfo hbi = Chenxin.Lib.House.DAL.House_BaseInfo.GetModel(hmi.HouseId);
                return string.Format("{{success:'1',msg:'成功',data:{{model:{0},house:{1}}}}}", Chenxin.Lib.Utility.JsonHelp.Convert2Json(hmi), Chenxin.Lib.Utility.JsonHelp.Convert2Json(hbi));
            }
            else
            {
                return string.Format("{{success:'0',msg:'{0}',data:''}}", "用户不存在！");
            }
        }
        catch (Exception ex)
        {
            return string.Format("{{success:'0',msg:'意外错误：{0}',data:''}}", ex.Message);
        }
    }

    private string edit(HttpContext context)
    {
        try
        {
            string modelid = context.Request["personCode"];
            Chenxin.Lib.House.Entity.House_MarkedInfo model = (Chenxin.Lib.House.Entity.House_MarkedInfo)Chenxin.Lib.Utility.JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.House.Entity.House_MarkedInfo));
            Chenxin.Lib.Passport.Entity.Hr_UserInfo hui = Chenxin.Lib.Passport.DAL.Hr_UserInfo.GetEntityByCode(modelid);
            Chenxin.Lib.House.Entity.House_MarkedInfo hmi = Chenxin.Lib.House.DAL.House_MarkedInfo.GetModel(model.RecordId);
            
            string log = CurrentUserInfo.UserName + "将备案号为：[" + model.MarkCode + "]的";
            if (hmi.MarkPerson != hui.UserName)
                log = log + " 备案所属人由[" + hmi.MarkPerson + "]变为[" + hui.UserName + "]";
            //log = log + " 所属人ID由[" + hmi.MarkPersonID + "]变为[" + hui.UserId + "]";
            if (hmi.MarkDepart != hui.DepartName)
                log = log + " 部门由[" + hmi.MarkDepart + "]变为[" + hui.DepartName + "]";
            //log = log + " 部门ID由[" + hmi.MarkDepartId + "]变为[" + hui.DepartId + "]";
            if (hmi.CompanyName != hui.CompanyName)                
                log = log + " 公司由[" + hmi.CompanyName + "]变为[" + hui.CompanyName + "]";
            //log = log + " 公司ID由[" + hmi.CompanyId + "]变为[" + hui.CompanyId + "]";
            if (hmi.MarkCode != model.MarkCode)
            {
                log = log + " 备案号由[" + hmi.MarkCode + "]变为[" + model.MarkCode + "]";
                Chenxin.Lib.House.Entity.House_BaseInfo houseinfo = Chenxin.Lib.House.DAL.House_BaseInfo.GetModel(hmi.HouseId);
                houseinfo.MarkCode = model.MarkCode;
                Chenxin.Lib.House.DAL.House_BaseInfo.Update(houseinfo);
            }
            if (hmi.PropertyCode != model.PropertyCode)
                log = log + " 产权证号由[" + hmi.PropertyCode + "]变为[" + model.PropertyCode + "]";
            if(hmi.PropertyPersonName!=model.PropertyPersonName)
                log = log + " 产权人名由[" + hmi.PropertyPersonName + "]变为[" + model.PropertyPersonName + "]";
            hmi.MarkDepart = hui.DepartName;
            hmi.MarkDepartId = hui.DepartId;
            hmi.MarkPerson = hui.UserName;
            hmi.MarkPersonID = hui.UserId;
            hmi.CompanyId = hui.CompanyId;
            hmi.CompanyName = hui.CompanyName;
            hmi.MarkCode = model.MarkCode;
            hmi.PropertyCode = model.PropertyCode;
            hmi.PropertyPersonName = model.PropertyPersonName;
            Chenxin.Lib.House.DAL.House_MarkedInfo.Update(hmi);
            AddLog(log,"备案变更");
            //添加跟单记录
            Chenxin.Lib.House.Entity.House_TraceInfo house_TraceInfo = new Chenxin.Lib.House.Entity.House_TraceInfo();
            //跟单方式
            house_TraceInfo.TraceType = 0;
            //跟单内容	 
            house_TraceInfo.TraceContext = log;
            //房源Id	 
            house_TraceInfo.HouseId = hmi.HouseId;
            //房源编号	
            house_TraceInfo.HouseCode = context.Request["HouseCode"];
            //房源地址	 
            house_TraceInfo.HouseAddress = hmi.HouseAddress;
            house_TraceInfo.status = 0;
            house_TraceInfo.OprDepart = hui.DepartName;
            house_TraceInfo.OprDepartId = hui.DepartId;
            house_TraceInfo.OprPerson = hui.UserName;
            house_TraceInfo.OprPersonId = hui.UserId;
            AddTrace(house_TraceInfo);
            return string.Format("{{success:'1',msg:'{0}',data:''}}", "变更备案成功！");
        }
        catch (Exception ex)
        {
            return string.Format("{{success:'0',msg:'意外错误：{0}',data:''}}", ex.Message);
        }
    }

    public override bool CustomerCheckPermint(HttpContext context, out string strMsg)
    {
        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        string op = context.Request.Params["op"];

        if (String.IsNullOrEmpty(op))
        {
            strMsg = "{success:0,msg:'操作参数有误！',data:''}";
            return false;
        }
        op = op.ToLower();

        string code = context.Request.Params["code"];
        if (op.Equals("checkpermit"))
        {
            if (String.IsNullOrEmpty(code))
            {
                strMsg = "{success:0,msg:'权限检查参数有误！',data:''}";
                return false;
            }
            code = code.ToLower();
            //权限判断
            //传递参数用
            System.Collections.Hashtable per = new System.Collections.Hashtable();
            System.Collections.Hashtable confine = new System.Collections.Hashtable();

            //编辑客户
            if (code.Equals("edit"))
            {
                if (CurrentUserInfo.ContainsPermit(Constants.Permit_EditMarkedCode))
                {
                    strMsg = "{success:1,msg:'',data:{permit:{editall:true}}}";
                    return true;
                }
                else
                {
                    strMsg = "{success:1,msg:'',data:{permit:{editall:false}}}";
                    return true;
                }
            }
        }
        strMsg = "{success:1,msg:'',data:{permit:true}}";
        return true;
    }
}